package com.google.code.gaeapptest.junit;

import java.util.Date;

import com.google.code.gaeapptest.dao.Dao;
import com.google.code.gaeapptest.dao.DaoFactory;

public class BaseTest {

	// saves the results of a test to the Results Table
	public static void saveResult(String category, long elapsedMs, String msg) {
		String testName = testName();
		Dao dao = DaoFactory.getInstance();
		dao.saveResult(new Date(), category, testName, elapsedMs, msg);
	}

	// returns the method name that called saveResult
	private static String testName() {
		StackTraceElement e[] = Thread.currentThread().getStackTrace();
		boolean doNext = false;
		String result = "?";
		for (StackTraceElement s : e) {
			if (doNext) {
				result = s.getMethodName().trim();
				break;
			}
			doNext = s.getMethodName().equals("saveResult");
		}
		return result;
	}

	public static GaeTest getInstance(String testClass) {
		GaeTest result = null;
		if (testClass != null) {
			testClass = testClass.toLowerCase();
			if (testClass.startsWith("blob")) {
				result = new BlobTests();
			} else if (testClass.startsWith("entity")) {
				result = new EntityTests();
			} else if (testClass.startsWith("file")) {
				result = new FileTests();
			} else if (testClass.startsWith("transaction")) {
				result = new TransactionTests();
			} else if (testClass.startsWith("sql")) {
				result = new SqlTests();
			}
		}
		return result;
	}
}
